using System;
using Kingdee.BOS.Core.Bill.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Core.Metadata.EntityElement;
using System.Data;
using Kingdee.BOS.ServiceHelper;

namespace QL.K3.SCM.SAL.CLIENT.BusinessPlugIn
{
    public class XiaoShouHeTong : AbstractBillPlugIn
    {


        /// <summary>
        /// 初始化，对其他界面传来的参数进行处理，对控件某些属性进行处理
        /// 这里不宜对数据DataModel进行处理
        /// </summary>
        /// <param name="e"></param>
        public override void OnInitialize(InitializeEventArgs e)
        {

        }

        public override void BeforeDeleteRow(BeforeDeleteRowEventArgs e)
        {
            // 单据体Key
            string entityKey = e.EntityKey;
            // 单据体元数据
            Entity entity = this.View.BillBusinessInfo.GetEntity(entityKey);
     
            // 被删除的行
            DynamicObject row = this.Model.GetEntityDataObject(entity, e.Row);
            // 被删除行的内码：可以据此到数据库搜索此行的原始内容
            long entityId = Convert.ToInt64(row[0]);

            //this.View.ShowMessage(row["GuiGe"].ToString(), MessageBoxType.Notice);
            //this.View.ShowMessage(e.Row.ToString(), MessageBoxType.Notice);


          
            string sql0 =
                "  select 1 from t_sal_xiaoshouhetongEntry a where exists( "
                + " select 1 from( "
                + " select FHETONGWULIAOFENZU from T_ProduceSingleEntry "
                + " union "
                + " select FHETONGWULIAOFENZU from T_SAL_ORDERENTRY "
                + " union "
                + " select FHETONGWULIAOFENZU  from T_SAL_RETURNSTOCKENTRY "
                + " union "
                + " select FHETONGWULIAOFENZU  from T_SAL_OUTSTOCKENTRY "
                + " ) b where a.fentryid = b.FHETONGWULIAOFENZU) "
                + " and fentryid = " + entityId;

            IDataReader rs = DBServiceHelper.ExecuteReader(this.Context, sql0);


            while (rs.Read())
            {
                this.View.ShowMessage("删除失败，已有关联单据不允许删除行。");
                e.Cancel = true;
            }
            rs.Close();

         


            //if (entityId != 0)
            //{
            //    double yidinghuoshuliang = Convert.ToDouble(row["yidinghuoshuliang"]);
            //    double yibaochanshuliang = Convert.ToDouble(row["yibaochanshuliang"]);

            //    if(yidinghuoshuliang!=0 || yibaochanshuliang!=0)
            //    {
            //        this.View.ShowMessage("删除失败，已订货数量或已报产数量不等于，不允许删除。");
            //        e.Cancel = true;
            //    }      
            //}
       
        }


       
    }


}
